package com.government.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.common.core.domain.entity.SysUser;
import org.springframework.transaction.annotation.Transactional;
import com.government.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.system.mapper.GuestbookMapper;
import com.government.system.domain.Guestbook;
import com.government.system.service.IGuestbookService;

import static com.government.common.utils.PageUtils.startPage;
import static com.government.common.utils.SecurityUtils.getLoginUser;

/**
 * 留言板Service业务层处理
 *
 * @author system
 * @date 2023-11-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GuestbookServiceImpl implements IGuestbookService
{
    @Autowired
    private GuestbookMapper guestbookMapper;

    /**
     * 查询留言板
     *
     * @param id 留言板主键
     * @return 留言板
     */
    @Override
    public List<Guestbook> selectGuestbookByUserId(Long userId)
    {
        return guestbookMapper.selectList(Wrappers.<Guestbook>lambdaQuery().eq(Guestbook::getUserId,userId));
    }

    /**
     * 查询留言板列表
     *
     * @param guestbook 留言板
     * @return 留言板
     */
    @Override
    public List<Guestbook> selectGuestbookList(Guestbook guestbook)
    {
        startPage();
        return guestbookMapper.selectGuestbookList(guestbook);
    }

    /**
     * 新增留言板
     *
     * @param guestbook 留言板
     * @return 结果
     */
    @Override
    public int insertGuestbook(Guestbook guestbook)
    {
        SysUser user = getLoginUser().getUser();
        if("1".equals(guestbook.getType())){
            guestbook.setUserId(user.getUserId());
        }

        guestbook.setSendId(user.getUserId());
        guestbook.setCreateTime(DateUtils.getNowDate());
        return guestbookMapper.insert(guestbook);
    }

}
